import global, { Json_skill, Skill } from "../../global";
import Big, { Evolution } from "../battle/big";
import Point_ts from "../battle/point";
import { Invest } from "../battle/small";

const {ccclass, property} = cc._decorator;

@ccclass
export default class GameSkill extends cc.Component {

    @property(cc.Sprite)
    Sprite:cc.Sprite = null;    //技能图片
    @property(cc.Sprite)
    Sprite_filter:cc.Sprite = null;     //状态滤镜
    @property(cc.Label)
    Label_cooling:cc.Label = null;      //剩余冷却
    @property(cc.Node)
    Node_hold:cc.Node = null;         //充能
    @property(cc.Label)
    Label_hold:cc.Label = null;        //充能

    @property(cc.SpriteFrame)
    SF_cooling:cc.SpriteFrame = null;   //冷却中
    @property(cc.SpriteFrame)
    SF_locking:cc.SpriteFrame = null;   //未解锁
    @property(cc.SpriteFrame)
    SF_destory:cc.SpriteFrame = null;   //已销毁

    skill:Skill = null;
    json_skill:Json_skill = null;
    
    is_broken:boolean = false;          //已经破碎

    onLoad(){
        this.Label_cooling.node.active = false;
        this.Sprite_filter.spriteFrame = this.SF_locking;
    }

    /**
     * 换图片
     */
    init(skill:Skill){
        this.skill = skill;
        this.json_skill = {...skill};
        global.loadSf(this.Sprite,`/skill/${this.skill.bh}`);
        //只能储存一次，隐藏这个节点
        if(skill.hold>1){
            this.skill.hold = 1;
            this.Label_hold.string = ""+this.skill.hold;
        }else{
            this.Node_hold.active = false;
        }
    }

    /**
     * 点击
     */
    click(){
        //先判断是否是自己的回合
        if(global.mgr.turn_player.id!=global.id){
            return;
        }
        //先判断该技能是否已经解锁
        if(!this.skill.is_unlock){
            global.frame.openFrameSkillActive(this);
            return;
        }
        //判断是否已经破碎
        if(this.is_broken){
            global.persist.tip("已破碎，不可用");
            return;
        }
        //检查是否可以使用 充能数>0就可以用
        if(this.skill.hold<1){
            global.persist.tip("冷却中");
            return;
        }
        //使用技能
        global.mgr.skill_ts = this;
        //取消全部事件监听
        global.mgr.clear();
        switch(this.skill.bh){
            case 0:this.csjj();break;
            case 1:this.xysz();break;
            case 2:this.chdj();break;
            case 3:this.pzjp();break;
            case 4:this.gzcg();break;
            case 7:this.dysf();break;
            case 9:this.qxbs();break;
            case 10:this.mhsg();break;
            case 12:this.sxzh();break;
            case 13:this.jmxj();break;
            case 15:this.gdfl();break;
            case 16:this.bdsl();break;
            case 17:this.ssds();break;
            case 19:this.wycb();break;
            case 20:this.smwz();break;
            case 21:this.cfpl();break;
            case 23:this.gbwc();break;
            case 25:this.qszh();break;
            case 27:this.bsdf();break;
            case 28:this.jqhh();break;
            case 29:this.ssbx();break;
            case 31:this.ffcb();break;
            case 33:this.tltx();break;
            case 35:this.jsbm();break;
            case 37:this.tycz();break;
            case 38:this.sbtj();break;
            case 42:this.mwsl();break;
            case 43:this.elfs();break;
            case 44:this.fxbs();break;
            case 45:this.jyzz();break;
            case 46:this.sxmn();break;
            case 47:this.jdzz();break;
            case 48:this.bdzz();break;
            case 50:this.tysd();break;
            case 51:this.wedt();break;
            case 52:this.jtsk();break;
            case 53:this.cpzj();break;
            case 54:this.zgds();break;
            default:console.log("没有这个技能");
        }
    }

    /**
     * 使用了一次
     */
    used(){
        this.skill.hold--;
        if(this.skill.hold>0){
            this.canUse();
        }else{
            if(this.skill.time!=0){
                this.skill.time--;
                if(this.skill.cooling==0){
                    this.skill.cooling = this.json_skill.cooling;
                }
                this.cooling();
            }else{
                this.is_broken = true;
                this.broken();
            }
        }
        //播放音效
        global.music_ts.playEffect("Audio_skillUsed");
    }


    /**
     * 一天过去
     */
    oneDay(){
        //没有解锁不执行
        if(!this.skill.is_unlock){
            return;
        }
        //已破碎不执行
        if(this.is_broken){
            return;
        }
        //充能已满,不执行
        if(this.skill.hold==this.json_skill.hold){
            return;
        }
        //冷却-1
        this.skill.cooling--;
        if(this.skill.cooling==0){
            this.skill.hold++;
            //充能没满要继续冷却
            if(this.skill.hold!=this.json_skill.hold){
                //检查使用次数是否足够开始一次新的冷却
                if(this.skill.time<this.json_skill.time){
                    this.skill.cooling = this.json_skill.cooling;
                }
            }
            this.canUse();
        }else{
            this.cooling();
        }
    }

    /**
     * 进入冷却
     */
    cooling(){
        this.Sprite_filter.node.active = true;
        this.Sprite_filter.spriteFrame = this.SF_cooling;
        this.Label_cooling.node.active = true;
        this.Label_cooling.string = this.skill.cooling+"";
    }

    /**
     * 破碎
     */
    broken(){
        this.node.getComponent(cc.Button).interactable = false;
        this.Sprite_filter.node.active = true;
        this.Sprite_filter.spriteFrame = this.SF_destory;
        this.Label_cooling.node.active = false;
        //播放音效
        global.music_ts.playEffect("Audio_skillBroken");
    }

    /**
     * 可用
     */
    canUse(){
        this.Sprite_filter.node.active = false;
        this.Label_cooling.node.active = false;
    }

    /****************************************以下是技能逻辑******************************************************* */

    /**
     * 常胜将军
     */
    csjj(){
        //全部大子开始监听
        global.mgr.me_player.arr_big.forEach(big_ts=>{
            big_ts.oncsjj();
        });
    }

    /**
     * 骁勇善战
     */
    xysz(){
        //全部大子开始监听
        global.mgr.me_player.arr_big.forEach(big_ts=>{
            big_ts.onxysz();
        });
    }

    /**
     * 趁火打劫
     */
    chdj(){
        global.client.send({method:"skill",skill:"chdj",id:global.id,bh:global.mgr.bh+1});
        this.used();
    }

    /**
     * 炮转狙炮
     */
    pzjp(){
        //全部转型成炮的大子开始监听
        let is_havePT = false;
        //这里先判断有没有炮
        global.mgr.me_player.arr_big.forEach(big_ts=>{
            if(big_ts.evolution==Evolution.Battery){
                big_ts.onpzjp()
                is_havePT = true;
            }
        });
        if(!is_havePT){
            global.mgr.me_player.homing();
            global.persist.tip("没有炮台可以施加符咒");
        }
    }

    /**
     * 鬼转超鬼
     */
    gzcg(){
        //全部转型成鬼的大子开始监听
        let is_haveYL = false;
        global.mgr.me_player.arr_big.forEach(big_ts=>{
            if(big_ts.evolution==Evolution.Ghost){
                big_ts.ongzcg();
                is_haveYL = true;
            }
        });
        if(!is_haveYL){
            global.mgr.me_player.homing();
            global.persist.tip("没有鬼可以转");
        }
    }

    /**
     * 地狱杀伐
     */
    dysf(){
        //视野内point开启监听
        let arr_pointTs = global.mgr.getArrPointView();
        arr_pointTs.forEach(point_ts=>{
            point_ts.ondysf();
        });
    }

    /**
     * 潜行捕手
     */
    qxbs(){
        //全部大子开始监听
        global.mgr.me_player.arr_big.forEach(big_ts=>{
            big_ts.onqxbs();
        });
    }

    /**
     * 明魂搜根
     */
    mhsg(){
        //全部可见敌方大子开启监听
        let arr_pointTs = global.mgr.getArrPointView();
        arr_pointTs.forEach(point_ts=>{
            let big = point_ts.node.getChildByName("大子");
            if(big){
                let big_ts = big.getComponent(Big);
                if(big_ts.player.id!=global.id&&big.active){
                    big_ts.onmhsg();
                }
            }
        });
    }

    /**
     * 属性置换
     */
    sxzh(){
        global.mgr.me_player.arr_big.forEach(big_ts=>{
            big_ts.onsxzh();
        });
    }

    /**
     * 绝命献祭
     */
    jmxj(){
        global.mgr.me_player.arr_big.forEach(big_ts=>{
            big_ts.onjmxj();
        });
    }

    /**
     * 过度分裂
     */
    gdfl(){
        global.mgr.me_player.arr_big.forEach(big_ts=>{
            big_ts.ongdfl();
        });
    }

    /**
     * 冰冻手雷
     */
    bdsl(){
        //视野内point开启监听
        let arr_pointTs = global.mgr.getArrPointView();
        arr_pointTs.forEach(point_ts=>{
            point_ts.onbdsl();
        });
    }

    /**
     * 审时度势
     */
    ssds(){
        if(global.mgr.me_player.action>=4){
            global.client.send({method:"skill",skill:"ssds",id:global.id,bh:global.mgr.bh+1});
            global.persist.tip("献祭成功");
            this.used();
        }else{
            global.persist.tip("行动力不足");
        }
    }

    /**
     * 瘟疫传播
     */
    wycb(){
        let arr_pointTs = global.mgr.getArrPointView();
        arr_pointTs.forEach(point_ts=>{
            let big_ts = this.getPointBigTs(point_ts);
            if(big_ts){
                big_ts.onwycb();
            }
        });
    }

    /**
     * 检查是否是敌方未转型不隐身大子
     */
    getPointBigTs(point_ts:Point_ts){
        let big = point_ts.node.getChildByName("大子");
        if(big){
            let big_ts = big.getComponent(Big);
            if(big.active&&big_ts.evolution==Evolution.Null&&big_ts.player.id!=global.id){  //未转型 隐身 敌方
                return big_ts;
            }else{
                return null;
            }
        }else{
            return null;
        }
    }

    /**
     * 沙漠王子
     */
    smwz(){
        //全部大子开始监听
        global.mgr.me_player.arr_big.forEach(big_ts=>{
            big_ts.onsmwz();
        });
    }

    /**
     * 乘风破浪
     */
    cfpl(){
        global.mgr.me_player.arr_big.forEach(big_ts=>{
            big_ts.oncfpl();
        });
    }

    /**
     * 诡变无常
     */
    gbwc(){
        //视野内point开启监听
        let arr_pointTs = global.mgr.getArrPointView();
        arr_pointTs.forEach(point_ts=>{
            point_ts.ongbwc();
        });
    }

    /**
     * 敲山震虎
     */
    qszh(){
        let arr_pointTs = global.mgr.getArrPointView();
        arr_pointTs.forEach(point_ts=>{
            point_ts.onqszh();
        });
    }

    /**
     * 搬山大法
     */
    bsdf(){
        //视野内point开启监听
        let arr_pointTs = global.mgr.getArrPointView();
        arr_pointTs.forEach(point_ts=>{
            point_ts.onbsdf();
        });
    }


    /**
     * 九曲黄河
     */
    jqhh(){
        //视野内point开启监听
        let arr_pointTs = global.mgr.getArrPointView();
        arr_pointTs.forEach(point_ts=>{
            point_ts.onjqhh();
        });
    }

    /**
     * 生生不息
     */
    ssbx(){
        global.client.send({method:"skill",skill:"ssbx",id:global.id,bh:global.mgr.bh+1});
        this.used();
    }

    /**
     * 佛法慈悲
     */
    ffcb(){
        //视野内point开启监听
        let arr_pointTs = global.mgr.getArrPointView();
        arr_pointTs.forEach(point_ts=>{
            point_ts.onffcb();
        });
    }

    /**
     * 迷雾森林
     */
    mwsl(){
        //视野内point开启监听
        let arr_pointTs = global.mgr.getArrPointView();
        arr_pointTs.forEach(point_ts=>{
            point_ts.onmwsl();
        });
    }

    /**
     * 桃李天下
     */
    tltx(){
        global.mgr.me_player.arr_big.forEach(big_ts=>{
            big_ts.ontltx();
        });
    }

    /**
     * 恶灵附身
     */
    elfs(){
        let is_haveYL = false;
        global.mgr.me_player.arr_big.forEach(big_ts=>{
            //转型过的不能施加spell
            if(big_ts.evolution==Evolution.Ghost){
                big_ts.onelfs();
                is_haveYL = true;
            }
        });
        if(!is_haveYL){
            global.persist.tip("没有幽灵可以施加符咒");
            global.mgr.me_player.homing();
        }
    }

    /**
     * 金身不灭 
     */
    jsbm(){
        global.mgr.me_player.arr_big.forEach(big_ts=>{
            big_ts.onjsbm();
        });
    }

    /**
     * 天眼传真
     */
    tycz(){
        global.mgr.map_point.forEach(point_ts => {
            point_ts.is_view = true;
            point_ts.display();
        });
        global.mgr.me_player.homing();
        this.used();
        global.client.send({method:"skillOnlyMe",skill:"tycz",id:global.id});
    }

    /**
     * 神兵天降
     */
    sbtj(){
        let arr_pointTs = global.mgr.getArrPointView();
        arr_pointTs.forEach(point_ts=>{
            point_ts.onsbtj();
        });
    }

    /**
     * 风雪冰霜
     */
    fxbs(){
        //视野内point开启监听
        let arr_pointTs = global.mgr.getArrPointView();
        arr_pointTs.forEach(point_ts=>{
            point_ts.onfxbs();
        });
    }

    /**
     * 绝育诅咒
     */
    jyzz(){
        let arr_pointTs = global.mgr.getArrPointView();
        arr_pointTs.forEach(point_ts=>{
            let big_ts = this.getPointBigTs(point_ts);
            if(big_ts){
                big_ts.onjyzz();
            }
        });
    }

    /**
     * 嗜血蛮牛
     */
    sxmn(){
        global.mgr.me_player.arr_big.forEach(big_ts=>{
            big_ts.onsxmn();
        });
    }

    /**
     * 剧毒之种
     */
    jdzz(){
        global.client.send({method:"skill",skill:"jdzz",id:global.id,bh:global.mgr.bh+1});
        this.used();
    }

    /**
     * 冰冻之种
     */
    bdzz(){
        global.client.send({method:"skill",skill:"bdzz",id:global.id,bh:global.mgr.bh+1});
        this.used();
    }

    /**
     * 天御圣灯
     */
    tysd(){
        global.mgr.me_player.arr_big.forEach(big_ts=>{
            big_ts.ontysd();
        });
    }

    /**
     * 万厄毒体
     */
    wedt(){
        global.mgr.me_player.arr_big.forEach(big_ts=>{
            big_ts.onwedt();
        });
    }


    /**
     * 狡兔三窟
     */
    jtsk(){
        //视野内point开启监听
        let arr_pointTs = global.mgr.getArrPointView();
        arr_pointTs.forEach(point_ts=>{
            point_ts.onjtsk();
        });
    }


    /**
     * 拆炮专家
     */
    cpzj(){
        global.mgr.me_player.arr_big.forEach(big_ts=>{
            big_ts.oncpzj();
        });
    }

    /**
     * 捉鬼大师
     */
    zgds(){
        global.mgr.me_player.arr_big.forEach(big_ts=>{
            big_ts.onzgds();
        });
    }
}
